package cn.iocoder.yudao.module.design.controller.admin.flowmd;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.flowmd.vo.*;
import cn.iocoder.yudao.module.design.convert.flowmd.FlowMdConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.flowmd.FlowMdDO;
import cn.iocoder.yudao.module.design.service.flowmd.FlowMdService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 流程模板")
@RestController
@RequestMapping("/design/flow-md")
@Validated
public class FlowMdController {

    @Resource
    private FlowMdService flowMdService;

    @PostMapping("/create")
    @Operation(summary ="创建流程模板")
    @PreAuthorize("@ss.hasPermission('design:flow-md:create')")
    public CommonResult<Long> createFlowMd(@Valid @RequestBody FlowMdCreateReqVO createReqVO) {
        return success(flowMdService.createFlowMd(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新流程模板")
    @PreAuthorize("@ss.hasPermission('design:flow-md:update')")
    public CommonResult<Boolean> updateFlowMd(@Valid @RequestBody FlowMdUpdateReqVO updateReqVO) {
        flowMdService.updateFlowMd(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除流程模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:flow-md:delete')")
    public CommonResult<Boolean> deleteFlowMd(@RequestParam("id") Long id) {
        flowMdService.deleteFlowMd(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得流程模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:flow-md:query')")
    public CommonResult<FlowMdRespVO> getFlowMd(@RequestParam("id") Long id) {
        FlowMdDO flowMd = flowMdService.getFlowMd(id);
        return success(FlowMdConvert.INSTANCE.convert(flowMd));
    }

    @GetMapping("/list")
    @Operation(summary ="获得流程模板列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:flow-md:query')")
    public CommonResult<List<FlowMdRespVO>> getFlowMdList(@RequestParam("ids") Collection<Long> ids) {
        List<FlowMdDO> list = flowMdService.getFlowMdList(ids);
        return success(FlowMdConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得流程模板分页")
    @PreAuthorize("@ss.hasPermission('design:flow-md:query')")
    public CommonResult<PageResult<FlowMdRespVO>> getFlowMdPage(@Valid FlowMdPageReqVO pageVO) {
        PageResult<FlowMdDO> pageResult = flowMdService.getFlowMdPage(pageVO);
        return success(FlowMdConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出流程模板 Excel")
    @PreAuthorize("@ss.hasPermission('design:flow-md:export')")
    public void exportFlowMdExcel(@Valid FlowMdExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FlowMdDO> list = flowMdService.getFlowMdList(exportReqVO);
        // 导出 Excel
        List<FlowMdExcelVO> datas = FlowMdConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "流程模板.xls", "数据", FlowMdExcelVO.class, datas);
    }

    @GetMapping("/getFlowMdListByCd")
    @Operation(summary ="根据模板类型获得流程模板列表")
    @Parameter(name = "flowCd", description = "模板编号", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:flow-md:query')")
    public CommonResult<List<FlowMdRespVO>> getFlowMdList(@RequestParam("flowCd") Integer flowCd){
        List<FlowMdDO> list = flowMdService.getFlowMdList(flowCd);
        return success(FlowMdConvert.INSTANCE.convertList(list));
    }

}
